package com.mbbb.yygh.user.controller;

import com.mbbb.yygh.common.result.R;
import com.mbbb.yygh.common.utils.AuthContextHolder;
import com.mbbb.yygh.common.utils.JwtHelper;
import com.mbbb.yygh.model.user.UserInfo;
import com.mbbb.yygh.user.service.UserInfoService;
import com.mbbb.yygh.vo.user.LoginVo;
import com.mbbb.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author mbbb
 * @since 2021-12-16
 */
@RestController
@RequestMapping("/api/user")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    //获取用户id信息接口
    @GetMapping("auth/getUserInfo")
    public R getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return R.ok().data("userInfo",userInfo);
    }

    //用户认证接口
    @PostMapping("auth/userAuth")
    public R userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        //方式一
        //Cookie[] cookies = request.getCookies();

        //方式二
        Long userId = AuthContextHolder.getUserId(request);

        //调用service方法进行认证更新
        userInfoService.userAuthUpdate(userId,userAuthVo);

        return R.ok();
    }

    @ApiOperation("用户登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {

        Map<String, Object> map = userInfoService.loginUser(loginVo);

        return R.ok().data(map);
    }
}

